﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;

namespace sistemaAcademicoFinanceiroEscolas
{
    public partial class agendaProfessor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqlc.Open();

                string sqlmes = "select * from mes";

                SqlCommand cmdmes = new SqlCommand(sqlmes, sqlc);
                SqlDataReader myReadermes = cmdmes.ExecuteReader();

                while (myReadermes.Read())
                {
                    ListItem mesSelecione = new ListItem();
                    mesSelecione.Value = myReadermes["codigomes"].ToString();
                    mesSelecione.Text = myReadermes["descricaomes"].ToString();
                    lstMes.Items.Add(mesSelecione);
                }
                cmdmes.Dispose();
                myReadermes.Close();

                string sqlAnoLetivo = "select * from anoLetivo";

                SqlCommand cmdAnoLetivo = new SqlCommand(sqlAnoLetivo, sqlc);
                SqlDataReader myReaderAnoLetivo = cmdAnoLetivo.ExecuteReader();

                while (myReaderAnoLetivo.Read())
                {
                    ListItem anoLetivoSelecione = new ListItem();
                    anoLetivoSelecione.Value = myReaderAnoLetivo["codigoAnoLetivo"].ToString();
                    anoLetivoSelecione.Text = myReaderAnoLetivo["descricaoAnoLetivo"].ToString();
                    lstAnoLetivo.Items.Add(anoLetivoSelecione);
                }
                cmdAnoLetivo.Dispose();
                myReaderAnoLetivo.Close();

                string sqlProfessor = "select * from professor order by nomeprofessor";

                SqlCommand cmdProfessor = new SqlCommand(sqlProfessor, sqlc);
                SqlDataReader myReaderProfessor = cmdProfessor.ExecuteReader();

                while (myReaderProfessor.Read())
                {
                    ListItem ProfessorSelecione = new ListItem();
                    ProfessorSelecione.Value = myReaderProfessor["codigoProfessor"].ToString();
                    ProfessorSelecione.Text = myReaderProfessor["nomeProfessor"].ToString();
                    lstProfessor.Items.Add(ProfessorSelecione);
                }
                cmdProfessor.Dispose();
                myReaderProfessor.Close();

                string sqlDia = "select * from dia";

                SqlCommand cmdDia = new SqlCommand(sqlDia, sqlc);
                SqlDataReader myReaderDia = cmdDia.ExecuteReader();

                while (myReaderDia.Read())
                {
                    ListItem DiaSelecione = new ListItem();
                    DiaSelecione.Value = myReaderDia["codigoDia"].ToString();
                    DiaSelecione.Text = myReaderDia["dia"].ToString();
                    lstDia.Items.Add(DiaSelecione);
                }
                cmdDia.Dispose();
                myReaderDia.Close();

                sqlc.Close();
            }
        }

        protected void btnVerificar_Click(object sender, EventArgs e)
        {
            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            lstgrupo.Items.Clear();
            lstgrupo2.Items.Clear();
            lstgrupo3.Items.Clear();
            lstgrupo4.Items.Clear();
            lstgrupo5.Items.Clear();

            lstmateria.Items.Clear();
            lstmateria2.Items.Clear();
            lstmateria3.Items.Clear();
            lstmateria4.Items.Clear();
            lstmateria5.Items.Clear();

            txtValor1.Text = "";
            txtValor2.Text = "";
            txtValor3.Text = "";
            txtValor4.Text = "";
            txtValor5.Text = "";

            txt1.Text = "";
            txt2.Text = "";
            txt3.Text = "";
            txt4.Text = "";
            txt5.Text = "";


            string sqlgrupo = "select * from grupo order by nomegrupo";

            SqlCommand cmdgrupo = new SqlCommand(sqlgrupo, sqlc);
            SqlDataReader myReadergrupo = cmdgrupo.ExecuteReader();

            ListItem primeiro = new ListItem();

            primeiro.Value = "0";
            primeiro.Text = "selecione";

            lstgrupo.Items.Add(primeiro);
            lstgrupo2.Items.Add(primeiro);
            lstgrupo3.Items.Add(primeiro);
            lstgrupo4.Items.Add(primeiro);
            lstgrupo5.Items.Add(primeiro);

            while (myReadergrupo.Read())
            {
                ListItem grupoSelecione = new ListItem();

                ListItem grupoSelecione2 = new ListItem();

                ListItem grupoSelecione3 = new ListItem();

                ListItem grupoSelecione4 = new ListItem();

                ListItem grupoSelecione5 = new ListItem();

                grupoSelecione.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione2.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione2.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione3.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione3.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione4.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione4.Text = myReadergrupo["nomegrupo"].ToString();

                grupoSelecione5.Value = myReadergrupo["codigogrupo"].ToString();
                grupoSelecione5.Text = myReadergrupo["nomegrupo"].ToString();

                lstgrupo.Items.Add(grupoSelecione);
                lstgrupo2.Items.Add(grupoSelecione2);
                lstgrupo3.Items.Add(grupoSelecione3);
                lstgrupo4.Items.Add(grupoSelecione4);
                lstgrupo5.Items.Add(grupoSelecione5);
            }
            cmdgrupo.Dispose();
            myReadergrupo.Close();

            string sqlDiscProf = "select p.codigoProfessor as codigoProfessor, p.nomeProfessor as nomeProfessor, d.codigoDisciplina as codigoDisciplina, ";
            sqlDiscProf = sqlDiscProf + "d.descricaodisciplina as descricaodisciplina, d.codigoSerie, dp.codigoProfessor, dp.codigoDisciplina, s.descricaoSerie as descricaoSerie, s.codigoSerie as codigoSerie";
            sqlDiscProf = sqlDiscProf + " from professor p,";
            sqlDiscProf = sqlDiscProf + " disciplinaProfessor dp, disciplina d, serie s ";
            sqlDiscProf = sqlDiscProf + " where dp.codigoDisciplina = d.codigoDisciplina and ";
            sqlDiscProf = sqlDiscProf + " dp.codigoProfessor = p.codigoProfessor and ";
            sqlDiscProf = sqlDiscProf + " p.codigoProfessor = " + lstProfessor.SelectedValue + " and d.codigoSerie = s.codigoSerie order by d.descricaoDisciplina";

            SqlCommand cmddisciplina = new SqlCommand(sqlDiscProf, sqlc);
            SqlDataReader myReaderDisc = cmddisciplina.ExecuteReader();


            lstmateria.Items.Add(primeiro);
            lstmateria2.Items.Add(primeiro);
            lstmateria3.Items.Add(primeiro);
            lstmateria4.Items.Add(primeiro);
            lstmateria5.Items.Add(primeiro);

            if (myReaderDisc.HasRows)
            {
                while (myReaderDisc.Read())
                {
                    ListItem materia = new ListItem();
                    ListItem materia2 = new ListItem();
                    ListItem materia3 = new ListItem();
                    ListItem materia4 = new ListItem();
                    ListItem materia5 = new ListItem();

                    materia.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia2.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia2.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia3.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia3.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia4.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia4.Text = myReaderDisc["descricaodisciplina"].ToString();

                    materia5.Value = myReaderDisc["codigoDisciplina"].ToString();
                    materia5.Text = myReaderDisc["descricaodisciplina"].ToString();

                    lstmateria.Items.Add(materia);
                    lstmateria2.Items.Add(materia2);
                    lstmateria3.Items.Add(materia3);
                    lstmateria4.Items.Add(materia4);
                    lstmateria5.Items.Add(materia5);
                }
            }

            myReaderDisc.Close();

            string trazerValor = "SELECT codigoValorAula, aulaValor, codigoTurno, codigoDiaLetivo, dataInicial, dataFinal FROM valorAula";

            SqlCommand cmdtrazerValor = new SqlCommand(trazerValor, sqlc);
            SqlDataReader drtrazerValor = cmdtrazerValor.ExecuteReader();


            string dia = lstDia.SelectedValue;
            if (dia.Length == 1)
            {
                dia = "0" + lstDia.SelectedValue;
            }

            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }

            string ano = lstAnoLetivo.SelectedItem.Text;

            if (drtrazerValor.HasRows)
            {
                while (drtrazerValor.Read())
                {
                    string data = dia + " / " + mes + " / " + ano;

                    if (Convert.ToDateTime(data) >= Convert.ToDateTime(drtrazerValor["dataInicial"].ToString()) && Convert.ToDateTime(data) < Convert.ToDateTime(drtrazerValor["dataFinal"].ToString()))
                    {
                        txtValor1.Text = drtrazerValor["aulaValor"].ToString();
                        txtValor2.Text = drtrazerValor["aulaValor"].ToString();
                        txtValor3.Text = drtrazerValor["aulaValor"].ToString();
                        txtValor4.Text = drtrazerValor["aulaValor"].ToString();
                        txtValor5.Text = drtrazerValor["aulaValor"].ToString();
                    }
                }
            }

            drtrazerValor.Close();


            System.Data.SqlClient.SqlConnection sqlj = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlj.Open();

            string selectAula = "SELECT codigoAula, codigoProfessor, codigoAluno, codigoGrupo, dataAula, horaInicialAula, ";
            selectAula += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, observacoesAula, ";
            selectAula += "aulaRealizada, valorHora, valorTotalSemDesconto, valorTotalComDesconto, falta, codigoAnoLetivo, linha, ";

            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (linha = '1')) AS horaInicial, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE (linha = '1')) AS horaFinal, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial2 FROM aula AS a WHERE (linha = '2')) AS horaInicial2, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal2 FROM aula AS aa WHERE (linha = '2')) AS horaFinal2, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial3 FROM aula AS a WHERE (linha = '3')) AS horaInicial3, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal3 FROM aula AS aa WHERE (linha = '3')) AS horaFinal3, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial4 FROM aula AS a WHERE (linha = '4')) AS horaInicial4, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal4 FROM aula AS aa WHERE (linha = '4')) AS horaFinal4, ";
            selectAula += "(SELECT MIN(horaInicialAula) AS horaInicial5 FROM aula AS a WHERE (linha = '5')) AS horaInicial5, ";
            selectAula += "(SELECT MAX(horaFinalAula) AS horaFinal5 FROM aula AS aa WHERE (linha = '5')) AS horaFinal5, ";
            selectAula += "(SELECT nomeAluno FROM aluno WHERE (codigoAluno = aula.codigoAluno)) AS aluno ";

            if (dia.Length == 1)
            {
                dia = "0" + dia;
            }
            if (mes.Length == 1)
            {
                mes = "0" + mes;
            }

            selectAula += "FROM aula WHERE (codigoProfessor = " + lstProfessor.SelectedValue + ") AND (dataAula = '" + dia + "/" + mes + "/" + ano + "', 0)";

            System.Data.SqlClient.SqlCommand cmdselectAula = new System.Data.SqlClient.SqlCommand(selectAula, sqlj);
            System.Data.SqlClient.SqlDataReader drselectAula = cmdselectAula.ExecuteReader();

            string hora1Inicial = "";
            string hora1Final = "";

            string hora2Inicial = "";
            string hora2Final = "";

            string hora3Inicial = "";
            string hora3Final = "";

            string hora4Inicial = "";
            string hora4Final = "";

            string hora5Inicial = "";
            string hora5Final = "";

            if (drselectAula.HasRows)
            {
                while (drselectAula.Read())
                {
                    hora1Inicial = drselectAula["horaInicial"].ToString();
                    hora1Final = drselectAula["horaFinal"].ToString();

                    hora2Inicial = drselectAula["horaInicial2"].ToString();
                    hora2Final = drselectAula["horaFinal2"].ToString();

                    hora3Inicial = drselectAula["horaInicial3"].ToString();
                    hora3Final = drselectAula["horaFinal3"].ToString();

                    hora4Inicial = drselectAula["horaInicial4"].ToString();
                    hora4Final = drselectAula["horaFinal4"].ToString();

                    hora5Inicial = drselectAula["horaInicial5"].ToString();
                    hora5Final = drselectAula["horaFinal5"].ToString();

                    switch (drselectAula["linha"].ToString())
                    {
                        case "1":
                            if (hora1Inicial != "" && hora1Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt1.Text = drselectAula["aluno"].ToString();
                                    hide1.Value = drselectAula["codigoAluno"].ToString();
                                }

                                txtValor1.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "2":
                            if (hora2Inicial != "" && hora2Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo2.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria2.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt2.Text = drselectAula["aluno"].ToString();
                                    hide2.Value = drselectAula["codigoAluno"].ToString();
                                }

                                txtValor2.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "3":
                            if (hora3Inicial != "" && hora3Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo3.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria3.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt3.Text = drselectAula["aluno"].ToString();
                                    hide3.Value = drselectAula["codigoAluno"].ToString();
                                }

                                txtValor3.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        case "4":
                            if (hora4Inicial != "" && hora4Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo4.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria4.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt4.Text = drselectAula["aluno"].ToString();
                                    hide4.Value = drselectAula["codigoAluno"].ToString();
                                }

                                txtValor4.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                        default:
                            if (hora5Inicial != "" && hora5Final != "")
                            {
                                if (drselectAula["codigoGrupo"].ToString() != "")
                                {
                                    lstgrupo5.SelectedValue = drselectAula["codigoGrupo"].ToString();
                                }
                                if (drselectAula["codigoDisciplina"].ToString() != "")
                                {
                                    lstmateria5.SelectedValue = drselectAula["codigoDisciplina"].ToString();
                                }
                                if (drselectAula["codigoAluno"].ToString() != "")
                                {
                                    txt5.Text = drselectAula["aluno"].ToString();
                                    hide5.Value = drselectAula["codigoAluno"].ToString();
                                }

                                txtValor5.Text = drselectAula["valorHora"].ToString();
                            }
                            break;
                    }
                }
            }

            drselectAula.Close();

            sqlj.Close();

            sqlc.Close();

            Panel1.Visible = true;
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            //Response.Write(hora1.Value + " " + hora12.Value);
            //Response.Write("<br>");
            //Response.Write(hide1.Value);
            //Response.Write("<br><br><br>");
            //Response.Write(hora2.Value + " " + hora22.Value);
            //Response.Write("<br>");
            //Response.Write(hide2.Value);
            //Response.Write("<br><br><br>");
            //Response.Write(hora3.Value + " " + hora32.Value);
            //Response.Write("<br>");
            //Response.Write(hide3.Value);
            //Response.Write("<br><br><br>");
            //Response.Write(hora4.Value + " " + hora42.Value);
            //Response.Write("<br>");
            //Response.Write(hide4.Value);
            //Response.Write("<br><br><br>");
            //Response.Write(hora5.Value + " " + hora52.Value);
            //Response.Write("<br>");
            //Response.Write(hide5.Value);
            //Response.Write("<br><br><br>");

            string hora1Inicial = "";
            string hora1Final = "";

            string hora2Inicial = "";
            string hora2Final = "";

            string hora3Inicial = "";
            string hora3Final = "";

            string hora4Inicial = "";
            string hora4Final = "";

            string hora5Inicial = "";
            string hora5Final = "";


            string dia = lstDia.SelectedValue;
            if (dia.Length == 1)
            {
                dia = "0" + lstDia.SelectedValue;
            }

            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }

            string ano = lstAnoLetivo.SelectedValue;
            if (ano.Length == 1)
            {
                ano = "0" + lstAnoLetivo.SelectedValue;
            }

            string selectVerificar = "SELECT codigoAula, codigoProfessor, codigoAluno, codigoGrupo, dataAula, horaInicialAula, ";
            selectVerificar += "horaFinalAula, codigoDisciplina, descricaoAssuntoAula, observacoesAula,  ";
            selectVerificar += "aulaRealizada, valorHora, valorTotalSemDesconto, valorTotalComDesconto, falta, codigoAnoLetivo, linha, ";

            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial FROM aula AS a WHERE (linha = '1') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal FROM aula AS aa WHERE (linha = '1')) AS horaFinal, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial2 FROM aula AS a WHERE (linha = '2') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial2, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal2 FROM aula AS aa WHERE (linha = '2') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal2, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial3 FROM aula AS a WHERE (linha = '3') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial3, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal3 FROM aula AS aa WHERE (linha = '3') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal3, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial4 FROM aula AS a WHERE (linha = '4') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial4, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal4 FROM aula AS aa WHERE (linha = '4') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal4, ";
            selectVerificar += "(SELECT MIN(horaInicialAula) AS horaInicial5 FROM aula AS a WHERE (linha = '5') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaInicial5, ";
            selectVerificar += "(SELECT MAX(horaFinalAula) AS horaFinal5 FROM aula AS aa WHERE (linha = '5') and (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "')) AS horaFinal5 ";

            selectVerificar += "FROM aula WHERE (dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "') AND (codigoProfessor = " + lstProfessor.SelectedValue + ") ";


            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            bool mensagem = true;

            //INSERT E VALIDAÇÕES REFERENTES A 1ª LINHA DE AGENDA DE AULA
            if (hide1.Value != "0" || lstgrupo.SelectedValue != "0")
            {
                if (hide1.Value != "0" && lstgrupo.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno

                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo1();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora1.Value;
                    string horaFinal = hora12.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }

                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais1();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;


                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '1'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        TimeSpan dif2 = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));


                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                dif2 = Convert.ToDateTime(drVerificar["horaFinal"].ToString()).Subtract(Convert.ToDateTime(drVerificar["horaInicial"].ToString()));

                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();


                                switch (drVerificar["linha"].ToString())
                                {
                                    case "2":
                                        if (hora2Inicial != "" && hora2Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;

                                    case "5":
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                                //if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}

                                //if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            if ((lstmateria.SelectedValue != "0"))
                            {
                                string sqlInserirAula = "";

                                if (hide1.Value != "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    if (dif.Hours > dif2.Hours)
                                    {
                                        string sqlDeletarAula = "delete from aula where linha = '1' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                        SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    if (dif.Hours < dif2.Hours)
                                    {
                                        string sqlDeletarAula = "delete from aula where linha = '1' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                        SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }

                                        string sqlDeletarAula = "delete from aula where horaInicialAula = '" + horaI + "' and horaFinalAula = '" + horaF + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }

                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + hide1.Value + ", " + ano + ", '" + txtValor1.Text + "', '1', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar1();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + ano + ", " + lstgrupo.SelectedValue + ", '" + txtValor1.Text + "', '1', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar1();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));

                                    mensagem = false;
                                }

                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe1();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }



            //INSERT E VALIDAÇÕES REFERENTES A 2ª LINHA DE AGENDA DE AULA
            if (hide2.Value != "0" || lstgrupo2.SelectedValue != "0")
            {
                if (hide2.Value != "0" && lstgrupo2.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno

                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo2();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora2.Value;
                    string horaFinal = hora22.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }
                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais2();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '2'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        //TimeSpan dif2 = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));


                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                //dif2 = Convert.ToDateTime(drVerificar["horaFinal2"].ToString()).Subtract(Convert.ToDateTime(drVerificar["horaInicial2"].ToString()));

                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();

                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "5":
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                                //if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}

                                //if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            if ((lstmateria2.SelectedValue != "0"))
                            {
                                string sqlInserirAula = "0";

                                if (hide2.Value == "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    //if (dif.Hours > dif2.Hours)
                                    //{
                                    //    string sqlDeletarAula = "delete from aula where linha = '2' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                    //    SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                    //    cmdInsert.ExecuteNonQuery();
                                    //}

                                    //if (dif.Hours < dif2.Hours)
                                    //{
                                    //    string sqlDeletarAula = "delete from aula where linha = '2' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                    //    SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                    //    cmdInsert.ExecuteNonQuery();
                                    //}


                                    string sqlDeletarAula = "delete from aula where linha = '2' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                    SqlCommand cmdDeletar = new SqlCommand(sqlDeletarAula, sqlc);
                                    cmdDeletar.ExecuteNonQuery();

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }

                                        sqlDeletarAula = "delete from aula where horaInicialAula = '" + horaI + "' and horaFinalAula = '" + horaF + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        cmdDeletar = new SqlCommand(sqlDeletarAula, sqlc);
                                        cmdDeletar.ExecuteNonQuery();
                                    }

                                    hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + ano + ", " + lstgrupo2.SelectedValue + ", '" + txtValor2.Text + "', '2', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }
                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar2();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria2.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + hide2.Value + ", " + ano + ", '" + txtValor2.Text + "', '2', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }
                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar2();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                            }

                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe2();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }



            //INSERT E VALIDAÇÕES REFERENTES A 3ª LINHA DE AGENDA DE AULA
            if (hide3.Value != "0" || lstgrupo3.SelectedValue != "0")
            {
                if (hide3.Value != "0" && lstgrupo3.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno

                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo3();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora3.Value;
                    string horaFinal = hora32.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }
                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais3();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '3'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();

                        TimeSpan dif2 = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));


                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {
                                dif2 = Convert.ToDateTime(drVerificar["horaFinal3"].ToString()).Subtract(Convert.ToDateTime(drVerificar["horaInicial3"].ToString()));


                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();



                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "2":
                                        if (hora2Inicial != "" && hora2Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "5":
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaInicial) <= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) >= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                                //if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}

                                //if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            if ((lstmateria3.SelectedValue != "0"))
                            {
                                string sqlInserirAula = "0";

                                if (hide3.Value == "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    if (dif.Hours > dif2.Hours)
                                    {
                                        string sqlDeletarAula = "delete from aula where linha = '3' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                        SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    if (dif.Hours < dif2.Hours)
                                    {
                                        string sqlDeletarAula = "delete from aula where linha = '3' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                        SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }

                                        string sqlDeletarAula = "delete from aula where horaInicialAula = '" + horaI + "' and horaFinalAula = '" + horaF + "' and codigoProfessor = " + lstProfessor.SelectedValue + " and dataAula = '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem.Text + "'";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlDeletarAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + ano + ", " + lstgrupo3.SelectedValue + ", '" + txtValor3.Text + "', '3', 0)";
                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }
                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar3();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria3.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + hide3.Value + ", " + ano + ", '" + txtValor3.Text + "', '3', 0)";
                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }
                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar3();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe3();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }



            //INSERT E VALIDAÇÕES REFERENTES A 4ª LINHA DE AGENDA DE AULA
            if (hide4.Value != "0" || lstgrupo4.SelectedValue != "0")
            {
                if (hide4.Value != "0" && lstgrupo4.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno

                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo4();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora4.Value;
                    string horaFinal = hora42.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }
                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais4();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '4'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();
                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {

                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();

                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "2":
                                        if (hora2Inicial != "" && hora2Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                }




                                //string horaInicials = drVerificar["horaInicialAula"].ToString();
                                //string horaFinals = drVerificar["horaFinalAula"].ToString();

                                //if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}

                                //if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            if ((lstmateria4.SelectedValue != "0"))
                            {
                                string sqlInserirAula = "0";

                                if (hide4.Value == "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + ano + ", " + lstgrupo4.SelectedValue + ", '" + txtValor4.Text + "', '4', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }
                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar4();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria4.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + hide4.Value + ", " + ano + ", '" + txtValor4.Text + "', '4', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }
                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar4();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe4();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }



            //INSERT E VALIDAÇÕES REFERENTES A 5ª LINHA DE AGENDA DE AULA
            if (hide5.Value != "0" || lstgrupo5.SelectedValue != "0")
            {
                if (hide5.Value != "0" && lstgrupo5.SelectedValue != "0")
                {
                    //validação selecione um grupo ou um aluno

                    //mensagem java script
                    string alunoGrupo = "<script language=JavaScript>alunoGrupo5();</script>";
                    Panel2.Controls.Add(new LiteralControl(alunoGrupo));
                }
                else
                {
                    string horaInicial = hora5.Value;
                    string horaFinal = hora52.Value;

                    if (horaInicial.Length == 4)
                    {
                        horaInicial = "0" + horaInicial;
                    }
                    if (horaFinal.Length == 4)
                    {
                        horaFinal = "0" + horaFinal;
                    }
                    TimeSpan dif = Convert.ToDateTime(horaFinal).Subtract(Convert.ToDateTime(horaInicial));

                    if (dif.Hours == 0)
                    {
                        //validação horas iguais

                        //mensagem java script
                        string horasIguais = "<script language=JavaScript>horasIguais5();</script>";
                        Panel2.Controls.Add(new LiteralControl(horasIguais));
                    }
                    else
                    {
                        bool validarInsert = true;

                        SqlCommand cmdVerificar = new SqlCommand(selectVerificar + " and linha <> '5'", sqlc);
                        SqlDataReader drVerificar = cmdVerificar.ExecuteReader();
                        if (drVerificar.HasRows)
                        {
                            while (drVerificar.Read())
                            {

                                hora1Inicial = drVerificar["horaInicial"].ToString();
                                hora1Final = drVerificar["horaFinal"].ToString();

                                hora2Inicial = drVerificar["horaInicial2"].ToString();
                                hora2Final = drVerificar["horaFinal2"].ToString();

                                hora3Inicial = drVerificar["horaInicial3"].ToString();
                                hora3Final = drVerificar["horaFinal3"].ToString();

                                hora4Inicial = drVerificar["horaInicial4"].ToString();
                                hora4Final = drVerificar["horaFinal4"].ToString();

                                hora5Inicial = drVerificar["horaInicial5"].ToString();
                                hora5Final = drVerificar["horaFinal5"].ToString();

                                switch (drVerificar["linha"].ToString())
                                {
                                    case "1":
                                        if (hora1Inicial != "" && hora1Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "2":
                                        if (hora2Inicial != "" && hora2Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial2"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal2"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (hora3Inicial != "" && hora3Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial3"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal3"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (hora4Inicial != "" && hora4Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial4"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal4"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        if (hora5Inicial != "" && hora5Final != "")
                                        {
                                            if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }

                                            if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicial5"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinal5"].ToString()))
                                            {
                                                validarInsert = false;
                                                break;
                                            }
                                        }
                                        break;
                                }
                                //if (Convert.ToDateTime(horaInicial) >= Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaInicial) < Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}

                                //if (Convert.ToDateTime(horaFinal) > Convert.ToDateTime(drVerificar["horaInicialAula"].ToString()) && Convert.ToDateTime(horaFinal) <= Convert.ToDateTime(drVerificar["horaFinalAula"].ToString()))
                                //{
                                //    validarInsert = false;
                                //    break;
                                //}
                            }
                        }
                        drVerificar.Close();

                        if (validarInsert)
                        {
                            if ((lstmateria5.SelectedValue != "0"))
                            {
                                string sqlInserirAula = "0";

                                if (hide5.Value == "0")
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAnoLetivo, codigoGrupo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + ano + ", " + lstgrupo5.SelectedValue + ", '" + txtValor5.Text + "', '5', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();
                                    }

                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar5();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                                else
                                {
                                    DateTime hI = Convert.ToDateTime(horaInicial);

                                    for (int i = 0; i < dif.Hours; i++)
                                    {
                                        DateTime hF = hI;
                                        hF += new TimeSpan(0, 0, 3600);

                                        string horaI = hI.ToString("HH:mm");
                                        string horaF = hF.ToString("HH:mm");

                                        if (hI.ToString().Length == 4)
                                        {
                                            horaI = "0" + hI.ToString("HH:mm");
                                        }
                                        if (hF.ToString().Length == 4)
                                        {
                                            horaF = "0" + hF.ToString("HH:mm");
                                        }
                                        sqlInserirAula = "INSERT INTO aula ";
                                        sqlInserirAula = sqlInserirAula + "(codigoDisciplina, codigoProfessor, horaInicialAula, horaFinalAula, dataAula, codigoAluno, codigoAnoLetivo, valorHora, linha, meiaHora)";
                                        sqlInserirAula = sqlInserirAula + " VALUES (" + lstmateria5.SelectedValue + ", " + lstProfessor.SelectedValue + ", ";
                                        sqlInserirAula = sqlInserirAula + " '" + horaI + "', '" + horaF + "', '" + dia + "/" + mes + "/" + lstAnoLetivo.SelectedItem + "', " + hide5.Value + ", " + ano + ", '" + txtValor5.Text + "', '5', 0)";

                                        if (Convert.ToDateTime(horaFinal) != hF)
                                        {
                                            hI = hF;
                                        }

                                        SqlCommand cmdInsert = new SqlCommand(sqlInserirAula, sqlc);
                                        cmdInsert.ExecuteNonQuery();

                                    }
                                    //mensagem java script
                                    string finalizar = "<script language=JavaScript>finalizar5();</script>";
                                    Panel2.Controls.Add(new LiteralControl(finalizar));
                                    mensagem = false;
                                }
                            }
                        }
                        else
                        {
                            //validar já existe aula nessas horas

                            //mensagem java script
                            string existe = "<script language=JavaScript>existe5();</script>";
                            Panel2.Controls.Add(new LiteralControl(existe));
                        }
                    }
                }
            }

            sqlc.Close();
        }

        protected void lstMes_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDia.Items.Clear();

            string mes = lstMes.SelectedValue;
            if (mes.Length == 1)
            {
                mes = "0" + lstMes.SelectedValue;
            }

            int pegarMes = Convert.ToInt16(mes);

            JulianCalendar cal = new JulianCalendar();

            int quantidadeDias = cal.GetDaysInMonth(1, pegarMes);

            int i = 1;
            for (i = 1; i <= quantidadeDias; i++)
            {
                ListItem dia = new ListItem();
                dia.Value = i.ToString();
                dia.Text = i.ToString();

                lstDia.Items.Add(dia);
            }
        }
    }
}
